package hander

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zhibo/zhibo/api/router"
	"zhibo/zhibo/global"
	"zhibo/zhibo/internal/model"
	"zhibo/zhibo/pkg"
)

func Login(c *gin.Context) {
	var req router.Login

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "参数验证失败",
			"data": nil,
		})
		return
	}
	user := model.User{
		Username: req.Username,
		Password: req.Password,
	}
	if err := global.DB.Find(&user).Error; err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "账号错误",
			"data": nil,
		})
		return
	}
	token, _ := pkg.NewJWT("2210a").CreateToken(pkg.CustomClaims{
		ID: user.ID,
	})

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": token,
	})
	return
}
